#include "serialwindow.h"
#include "ui_serialwindow.h"
#include <QDebug>
serialwindow::serialwindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::serialwindow)
{
    ui->setupUi(this);
    //先获取ports的相关信息
    QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
    //遍历找到port的端口号名字
    for(auto& port : ports)
    {
 //      qDebug()<<port.portName();
        ui->portNameBox->addItem(port.portName());
    }

    sPort = new QSerialPort;
}

serialwindow::~serialwindow()
{
    delete ui;
}

void serialwindow::on_openSerialPortButton_clicked()
{
    sPort->setPortName(ui->portNameBox->currentText());
    sPort->setBaudRate(9600);
    sPort->setDataBits(QSerialPort::Data8);
    sPort->setStopBits(QSerialPort::OneStop);
    sPort->setFlowControl(QSerialPort::NoFlowControl);
    sPort->setParity(QSerialPort::NoParity);

    if(sPort->open(QIODevice::ReadWrite))
    {
        ui->openSerialPortButton->setText("关闭串口");
    }
    else{
        QMessageBox::warning(this,"warning","open port fail");
        return;
    }

    connect(sPort,&QSerialPort::readyRead,[=](){
          ui->textEdit->append(sPort->readAll());
    });
}
